<template>
  <el-select
    v-model="value"
    filterable
    remote
    reserve-keyword
    :remote-method="customsMethod"
    :loading="optionLoading"
    clearable
    allow-create
    @change="change"
  >
    <el-option
      v-for="item in customsOptions"
      :key="item.customs_goods_code"
      :label="item.customs_goods_code"
      :value="item.customs_goods_code"
    ></el-option>
  </el-select>
</template>
<script setup>
import { queryCustomsList } from "./api.js";

const props = defineProps({
  modelValue: [String, Number, Array],
});

const value = computed(() => {
  return props.modelValue;
});

const customsOptions = ref([]);
const optionLoading = ref(false);
const customsMethod = async (query) => {
  if (query) {
    const { data } = await queryCustomsList({
      page: 1,
      page_size: 20,
      code: query,
    });
    customsOptions.value = data || [];
  } else {
    customsOptions.value = [];
  }
};

const emit = defineEmits(["update:modelValue"]);
const change = (val) => {
  const item = customsOptions.value.find((i) => i.customs_goods_code == val);
  emit("update:modelValue", val);
  emit("changeItem", item || "");
};

defineExpose({ customsMethod });
</script>
